from marshmallow import Schema, fields, validate
from datetime import datetime

class PhysicalExamSchema(Schema):
    """体格检查模式"""
    bloodPressure = fields.Str()
    pulse = fields.Str()
    temperature = fields.Str()
    weight = fields.Str()
    height = fields.Str()

class DiagnosisSchema(Schema):
    """诊断模式"""
    code = fields.Str()
    name = fields.Str(required=True)
    type = fields.Str(validate=validate.OneOf(['初步诊断', '确诊', '待排除', '鉴别诊断']))
    description = fields.Str()

class MultimediaSchema(Schema):
    """多媒体资料模式"""
    fileId = fields.Str(required=True)
    type = fields.Str(validate=validate.OneOf(['image', 'document', 'video', 'audio']))
    description = fields.Str()

class MedicalRecordSchema(Schema):
    """医疗记录模式"""
    recordId = fields.Str()
    patientId = fields.Str(required=True)
    visitId = fields.Str(required=True)
    visitDate = fields.DateTime(required=True)
    departmentId = fields.Str(required=True)
    doctorId = fields.Str(required=True)
    chiefComplaint = fields.Str(required=True)
    presentIllness = fields.Str(required=True)
    pastHistory = fields.Str()
    physicalExam = fields.Nested(PhysicalExamSchema)
    diagnosis = fields.List(fields.Nested(DiagnosisSchema), required=True)
    treatmentPlan = fields.Str(required=True)
    prescriptions = fields.List(fields.Str())
    multimedia = fields.List(fields.Nested(MultimediaSchema))
    status = fields.Str(validate=validate.OneOf(['草稿', '已完成', '已签名']), dump_default='草稿')
    version = fields.Integer(dump_default=1)
    previousVersions = fields.List(fields.Str())
    createdAt = fields.DateTime(dump_default=datetime.utcnow)

class MedicalRecordUpdateSchema(Schema):
    """医疗记录更新模式"""
    chiefComplaint = fields.Str()
    presentIllness = fields.Str()
    pastHistory = fields.Str()
    physicalExam = fields.Nested(PhysicalExamSchema)
    diagnosis = fields.List(fields.Nested(DiagnosisSchema))
    treatmentPlan = fields.Str()
    prescriptions = fields.List(fields.Str())
    multimedia = fields.List(fields.Nested(MultimediaSchema))
    status = fields.Str(validate=validate.OneOf(['草稿', '已完成', '已签名']))
    
class MedicalRecordQuerySchema(Schema):
    """医疗记录查询模式"""
    patientId = fields.Str()
    doctorId = fields.Str()
    departmentId = fields.Str()
    startDate = fields.Date()
    endDate = fields.Date()
    status = fields.Str(validate=validate.OneOf(['草稿', '已完成', '已签名', 'all'])) 